package com.orhon.smartcampus.modules.teacher.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSubject;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SubjectMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;

@Component
public class TeacherFetcher implements DataFetcher {
    
	@Autowired
    TInformationDao tInformationDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        //String token ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX3R5cGUiOiJzdHVkZW50Iiwic2Nob29sX2lkIjoxLCJleHAiOjE1NzI2Mzk1MDQsInVzZXJpZCI6MSwidXNlcm5hbWUiOiJ0ZWFjaGVyIn0.Z1lSJjXFhdyc6xwjmdFhTt6RzsFtkXyhsfc5vSXtpDw";
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from TInformation where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        TInformation item =  tInformationDao.findId(hql, null);
        if(item!=null) {
        	
            
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            
            GQLUser gqlUser = null;
            if (env.getSelectionSet().contains("user")){
               Users users = item.getUserUsersByUserId();
               gqlUser = UserMapper.MAPPER.mapUser(users);
            }
            List<GQLSubject> gqlSubject = null;
            if (env.getSelectionSet().contains("subjects")){
                List<Subjects> subject = item.getSubjects();
                gqlSubject = SubjectMapper.MAPPER.toSubjectList(subject);
                System.out.println("学科:"+gqlSubject.toString());
            }
            List<GQLGrades>  gqlGrades = null;
            if (env.getSelectionSet().contains("grades")){
                List<Grades> Grades = item.getGrades();
                gqlGrades = GradeMapper.MAPPER.toGradeList(Grades);
            }
            List<GQLPeriod>  gqlPeriod = null;
            if (env.getSelectionSet().contains("periods")){
                List<Periods> periods = item.getPeriods();
                gqlPeriod = PeriodMapper.MAPPER.toPeriodsList(periods);
            }
            
            GQLTeacher teacher = TeacherMapper.MAPPER.toTeacher(item);
            teacher.setUser(gqlUser);
            teacher.setSchool(gqlSchool);
            teacher.setSubjects(gqlSubject);
            teacher.setGrades(gqlGrades);
            teacher.setPeriods(gqlPeriod);
           
            return teacher;
        }
		return null;
        
    }


}
